//
//  ViewController.m
//  CoreImageDemo
//
//  Created by zero on 2017/6/9.
//  Copyright © 2017年 zero. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong) UIImageView* imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.imageView];
    
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(10, CGRectGetMaxY(self.imageView.frame)+50, 100, 40);
    [btn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside];
    [self findAllFilter];
}

- (void)findAllFilter{
    NSArray* array = [CIFilter filterNamesInCategory:kCICategoryBlur];
//    NSLog(@"%@",array);
    for (NSString* filterName in array) {
        NSLog(@"filter name:%@",filterName);
        // 我们可以通过filterName创建对应的滤镜对象
        CIFilter* filter = [CIFilter filterWithName:filterName];
        NSDictionary* attributes = [filter attributes];
        // 获取属性键/值对（在这个字典中我们可以看到滤镜的属性以及对应的key）
        NSLog(@"filter attributes:%@",attributes);
    }
}

- (void)change{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"p199012" ofType:@"jpg"];
    NSURL *fileNameAndPath = [NSURL fileURLWithPath:filePath];
    
    CIImage *beginImage = [CIImage imageWithContentsOfURL:fileNameAndPath];
    CIFilter *filter = [CIFilter filterWithName:@"CIMaskedVariableBlur" keysAndValues: kCIInputImageKey, beginImage, @"inputRadius", @1, nil];
    CIImage *outputImage = [filter outputImage];
    UIImage *newImage = [UIImage imageWithCIImage:outputImage];
    self.imageView.image = newImage;
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
}

- (UIImageView*)imageView{
    if(!_imageView){
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, CGRectGetWidth(self.view.frame)-20, CGRectGetWidth(self.view.frame)-20)];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.image = [UIImage imageNamed:@"p199012.jpg"];
    }
    return _imageView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
